
#include "palysecne.h"

palysecne::palysecne(QWidget *parent)
    : QMainWindow{parent}
{

}

palysecne::palysecne(int liveNo)
{
    this->setWindowTitle("第"+QString::number(liveNo)+"关");
    this->setFixedSize(320,588);
    this->setWindowIcon(QPixmap(":/new/coifilp/res/Coin0001.png"));
    QMenuBar* bar=menuBar();
    setMenuBar(bar);
    QMenu* startMenu=bar->addMenu("开始");
    QAction* quitAction=startMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,this,[=](){
        this->close();
        emit this->bktochoose();
    });
    myPushButton* mpbk=new myPushButton(":/new/coifilp/res/BackButton.png",":/new/coifilp/res/BackButtonSelected.png");
    mpbk->setParent(this);
    mpbk->move(this->width()-mpbk->width(),this->height()-mpbk->height());
    QSoundEffect* qsb=new QSoundEffect(this);
    qsb->setSource(QUrl::fromLocalFile(":/new/coifilp/res/BackButtonSound.wav"));
    connect(mpbk,&myPushButton::clicked,this,[=](){
        qsb->play();
        QTimer::singleShot(500,this,[=](){
            this->close();emit this->bktochoose();});});
    QLabel* ql=new QLabel(this);
    ql->setText("Level "+QString::number(liveNo));
    ql->setGeometry(this->width()*0.04,this->height()-ql->height()-this->height()*0.02,ql->width()*1.5,ql->height());
    QFont* qf=new QFont;
    qf->Bold;qf->Black;qf->setPointSize(25);
    ql->setFont(*qf);

    QLabel* winlable=new QLabel;
    QPixmap tmpPix;
    tmpPix.load(":/new/coifilp/res/LevelCompletedDialogBg.png");
    winlable->setGeometry(0,0,tmpPix.width(),tmpPix.height());
    winlable->setPixmap(tmpPix);
    winlable->setParent(this);
    winlable->move((this->width()-tmpPix.width())*0.5,-tmpPix.height());

    dataConfig* dac=new dataConfig(this);
    gameArray=dac->mData[liveNo];
    coibtn.resize(gameArray.count());
    for(int i=0;i<gameArray.count();i++){
        coibtn[i].resize(gameArray[0].count());
    }
//    qDebug()<<coibtn.count();
//    qDebug()<<coibtn[0].count();

    QSoundEffect* qsfl=new QSoundEffect(this);
    qsfl->setSource(QUrl::fromLocalFile(":/new/coifilp/res/ConFlipSound.wav"));
    QSoundEffect* qswin=new QSoundEffect(this);
    qswin->setSource(QUrl::fromLocalFile(":/new/coifilp/res/LevelWinSound.wav"));
    for(int i=0;i<gameArray.count();i++){
        for(int j=0;j<gameArray[0].count();j++){
            QPixmap* pix=new QPixmap(":/new/coifilp/res/BoardNode.png");
            QLabel* lable=new QLabel;
            lable->setGeometry(0,0,pix->width(),pix->height());
            lable->setPixmap(*pix);
            lable->setParent(this);
            int marign=(this->width()- gameArray[0].count()*pix->width())/2;
            lable->move(marign+i*50,200+j*50);
            QString str;
            if(gameArray[i][j]==1)
            {
                str=":/new/coifilp/res/Coin0001.png";
            }
            if(gameArray[i][j]==0)
            {
                str=":/new/coifilp/res/Coin0008.png";
            }
            myCoin* coin=new myCoin(str);
            coin->setParent(this);
//            coin->setGeometry(0,0,pix->width(),pix->height());
            coin->setParent(this);
            coin->move((marign+2)+i*50,204+j*50);
            coin->posX=i;
            coin->posY=j;
            coin->flag=gameArray[i][j];
            coibtn[i][j]=coin;
            connect(coin,&myCoin::clicked,coin,[=](){
                qsfl->play();
                coin->ChangFlag();
                gameArray[i][j]=gameArray[i][j]==0?1:0;
                if(coin->posX+1<gameArray[0].count()){
                    QTimer::singleShot(rand()%(400)+100,this,[=](){
                        coibtn[coin->posX+1][coin->posY]->ChangFlag();
                        gameArray[coin->posX+1][coin->posY]=gameArray[coin->posX+1][coin->posY]==0?1:0;
                    });
                }
                if(coin->posX-1>=0){
                    QTimer::singleShot(rand()%(400)+100,this,[=](){
                        coibtn[coin->posX-1][coin->posY]->ChangFlag();
                        gameArray[coin->posX-1][coin->posY]=gameArray[coin->posX-1][coin->posY]==0?1:0;
                    });
                }
                if(coin->posY+1<gameArray.count()){
                    QTimer::singleShot(rand()%(400)+100,this,[=](){
                        coibtn[coin->posX][coin->posY+1]->ChangFlag();
                        gameArray[coin->posX][coin->posY+1]=gameArray[coin->posX][coin->posY+1]==0?1:0;
                    });
                }
                if(coin->posY-1>=0){
                    QTimer::singleShot(rand()%(400)+100,this,[=](){
                        coibtn[coin->posX][coin->posY-1]->ChangFlag();
                        gameArray[coin->posX][coin->posY-1]=gameArray[coin->posX][coin->posY-1]==0?1:0;
                    });
                }
                isWin=true;
                QTimer::singleShot(rand()%(100)+500,this,[=](){
                    for(int i=0;i<gameArray.count();i++){
                        for(int j=0;j<gameArray[0].count();j++){
//                            qDebug()<<i<<j<<coibtn[i][j]->isWin;
//                            qDebug()<<"-----coibtn[i][j]->flag==false--------";
                            if(coibtn[i][j]->flag==false){
                                this->isWin=false;
                                break;
                            }
                        }
                    }
                });
                QTimer::singleShot(rand()%(100)+600,this,[=](){
                    if(this->isWin==true){
                        qswin->play();
                        for(int i=0;i<gameArray.count();i++){
                            for(int j=0;j<gameArray[0].count();j++){
                                coibtn[i][j]->isWin=true;
                            }
                        }
                        QPropertyAnimation* ain=new QPropertyAnimation(winlable,"geometry");
                        ain->setDuration(1000);
                        ain->setStartValue(QRect(winlable->x(),winlable->y(),winlable->width(),winlable->height()));
                        ain->setEndValue(QRect(winlable->x(),winlable->height()*1.25,winlable->width(),winlable->height()));
                        ain->setEasingCurve(QEasingCurve::OutBounce);
                        ain->start();
                    }

                });
            });
        }
    }

}
void palysecne::paintEvent(QPaintEvent *event)
{
    QPainter painterr(this);
    QPixmap pix;
    pix.load(":/new/coifilp/res/PlayLevelSceneBg.png");
    painterr.drawPixmap(0,0,this->width(),this->height(),pix);
    pix.load(":/new/coifilp/res/Title.png");
    pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painterr.drawPixmap(10,30,pix.width(),pix.height(),pix);


}



